//
//  InputManager.swift
//  LeMessage
//
//  Created by Rowling on 2020/3/10.
//  Copyright © 2020 Rowling. All rights reserved.
//

import Foundation
class InputMananger {
    static let shard = InputMananger()
    private init(){}
    let onlyNumber = NumberInputMananger()
    let noChinese = NoChineseInputMananger()
    let moneyInput = MoneyInput()
}

class NumberInputMananger : NSObject, UITextFieldDelegate {
    func manange(textFiled : UITextField)  {
        textFiled.delegate = self;
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let regex = "^[0-9]*$"
        let ok = NSPredicate(format: "SELF MATCHES %@",regex).evaluate(with: string)
        return ok
    }
}

class NoChineseInputMananger : NSObject, UITextFieldDelegate {
    func manange(textFiled : UITextField)  {
        textFiled.delegate = self;
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let regex = "^[^\\u4e00-\\u9fa5]*$"
        let ok = NSPredicate(format: "SELF MATCHES %@",regex).evaluate(with: string)
        return ok
    }
}


class MoneyInput : NSObject , UITextFieldDelegate {
   
        
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    //
//        let regex = "^[^\\u4e00-\\u9fa5]*$"
//        let ok = NSPredicate(format: "SELF MATCHES %@",regex).evaluate(with: string)
        let oldStr = textField.text
        let nsOldStr = oldStr! as NSString
        let newStr = nsOldStr.replacingCharacters(in: range, with: string)
        return (newStr.zm.match("\\d+\\.?\\d{0,2}") || newStr.count == 0)
    }
//    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//    {
//        NSString * oldStr = textField.text;
//        NSString * addStr = string;
//        NSString * newStr = [oldStr stringByReplacingCharactersInRange:range withString:string];
//        NSLog(@"\noldStr:%@\naddStr:%@\nnewStr:%@",oldStr,addStr,newStr);
//        return [newStr matchsRegex:@"\\d+\\.?\\d{0,2}"] || (newStr.length == 0);
//    }
}
